
const utils: any = {};

const setTp = (typ: string) => {
    return (obj: any) => (Object.prototype.toString.call(obj) === "[object " + typ + "]")
}

utils.isArray = setTp("Array");
utils.isFunction = setTp("Function");
utils.isObject = setTp("Object");
utils.isString = setTp("String");
utils.isNumber = setTp("Number");

// 校验数据
utils.check = {};
utils.check.mobile =  (data: string) => (/^(13|14|15|17|18|19)[0-9]{9}$/.test(data)),
utils.check.email = (data: string) => (/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/.test(data)),
utils.check.qq = (data: string) => (/^[1-9]([0-9]{5,11})$/.test(data))

utils.cookic = {
    get(key: string) {
        const arr = document.cookie.split('; ')
        for (const value of arr) {
            const arr2 = value.split('=')
            if (arr2[0] === key) {
                try {
                    return JSON.parse(arr2[1]);
                } catch (e) {
                    return arr2[1];
                }
            }
        }
        return ''
    },
    set(key: string, val: any, expirse: number = 7) {
        const oDate = new Date()
        oDate.setDate(oDate.getDate() + expirse)
        if (typeof val === 'object') {
            val = JSON.stringify(val)
        }
        document.cookie = key + '=' + val + ';expires=' + oDate + "; path=/"
    },
    clear(key: string) {
        this.setCookie(key, 1, -1)
    }
};


// 获取用户信息
utils.getUser = () =>{
    return utils.store.getCookie('userId');
}
// 设置用户信息
utils.setUser = (value: any) => {
    utils.store.setCookie("userId", value, 1);
}

// 清除用户信息
utils.clearUser = () => {
    utils.store.removeCookie("userId");
}


export default utils;